﻿#region

using System;
using System.Collections.Generic;
using MediaSage.Lib.Utilities;
using NHibernate;

#endregion

namespace MediaSage.Lib.Database
{
    public class ConfigEntity
    {
        #region Constructors

        private ISession configSession;

        public ConfigEntity()
        {
            FileSystems = new List<FileSystemEntity>();
            MediaTypes = new List<MediaFileTypesEntity>();
            Plugins = new List<PluginEntity>();
            lockObject = new object();
            configSession = DBUtil.GetSession().SessionFactory.OpenSession();
        }

        #endregion Constructors

        #region Properties

        protected Object lockObject;

        public virtual int RescanFrequency { get; set; }

        public virtual MediaMergeType MergeType { get; set; }

        public virtual bool MergeOnPersonName { get; set; }

        public virtual bool ConvertImages { get; set; }

        public virtual EImageFormat ConvertImageToType { get; set; }

        public virtual bool CreateImageThumbnails { get; set; }

        public virtual int DBLogLevel { get; set; }

        public virtual IList<FileSystemEntity> FileSystems { get; set; }

        public virtual bool ForceRescan { get; set; }

        public virtual bool GetAllImages { get; set; }

        public virtual int GuessScoreThreshold { get; set; }

        public virtual Guid ID { get; protected set; }

        public virtual string ImageDir { get; set; }

        public virtual string LogFile { get; set; }

        public virtual int LogLevel { get; set; }

        public virtual int MaxImageCount { get; set; }

        public virtual int MaxWorkerThreads { get; set; }

        public virtual IList<MediaFileTypesEntity> MediaTypes { get; set; }

        public virtual int NumberOfReturnedMoviesFromGuess { get; set; }

        public virtual string PluginDirectory { get; set; }

        public virtual IList<PluginEntity> Plugins { get; set; }

        public virtual int ScanFrequency { get; set; }

        public virtual int ThreadTimeout { get; set; }

        public virtual bool UpdateAutomatically { get; set; }

        public virtual string UpdateURL { get; set; }

        #endregion Properties

        #region Methods

        public virtual void AddMediaType(MediaFileTypesEntity mediaType)
        {
            MediaTypes.Add(mediaType);
        }

        public virtual void Delete()
        {
            ITransaction tranaction = configSession.BeginTransaction();
            configSession.Delete(this);
            tranaction.Commit();
            configSession.Flush();
        }

        public virtual void Save()
        {
            ITransaction tranaction = configSession.BeginTransaction();
            configSession.Save(this);
            tranaction.Commit();
            configSession.Flush();
        }

        public virtual void SaveOrUpdate()
        {
            ITransaction tranaction = configSession.BeginTransaction();
            configSession.SaveOrUpdate(this);
            tranaction.Commit();
            configSession.Flush();
        }

        #endregion Methods
    }
}